package com.heima.hellospring.service;

import com.heima.hellospring.domain.Teacher;
import com.heima.hellospring.repository.TeacherRepository;
import org.aspectj.weaver.Lint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
/**
 * created by tms
 * 2024-11-24
 * 教师服务类
 */
@Service
public class TeacherService {
    @Autowired
    private TeacherRepository teacherRepository;
    public List<Teacher> findAll() {
        return teacherRepository.findAll();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public Teacher findById(Integer id) {
        return teacherRepository.findById(id).orElse(null);
    }

    /**
     * 根据name查询
     * @param name
     * @return
     */
    public List<Teacher> findByTname(String name) {
        return teacherRepository.findByTnameLike(name);
    }

    /**
     *
     * 插入数据
     * @param teacher
     */
    public Teacher insert(Teacher teacher) {
        return teacherRepository.save(teacher);
    }

    /**
     *
     * 修改数据
     * @param teacher
     */
    public Teacher update(Teacher teacher) {
        return teacherRepository.save(teacher);
    }

    /**
     *
     * 删除数据
     * @param id
     */
    public void delete(Integer id) {
        teacherRepository.deleteById(id);
    }

    /**
     *
     * 查找职工号最大的教师数据
     * @param teachers
     */
    public List<Integer> queryByIds(List<Teacher> teachers) {
        int i,teacher_max=0;
        String s="",str = "";
        List<Integer> ids = new ArrayList<>();
        for(i=0;i<teachers.size();i++) {
            Teacher teacher = teachers.get(i);
            str=teacher.getTeacher_id().substring(0,4);
            int teacher_id = Integer.parseInt(str);
            if(teacher_max<teacher_id) {
                teacher_max = teacher_id;
            }
        }
        s=teacher_max+"";
        for(i=0;i<teachers.size();i++) {
            Teacher teacher = teachers.get(i);
            String str1=teacher.getTeacher_id().substring(0,4);
            if(str1.equals(s)) {
                ids.add(teacher.getId());
            }
        }
        return ids;
    }
}